// 封装localStorage,带入有效期
var storage = {
  // hours小时后失效，如果是-1或者空，表示不失效
  setVal(k, v, hours) {
    if (!v) {
      localStorage.removeItem(k)
    } else {
      var exp = -1
      if (hours) {
        exp = new Date().getTime() + hours * 60 * 60 * 1000
      }
      localStorage.setItem(k, JSON.stringify({
        val: v,
        expire: exp
      }))
    }
  },
  getVal(k) {
    try {
      var o = JSON.parse(localStorage.getItem(k))
      if (!o) {
        console.log('value object is null')
        return null
      }
      var now = new Date().getTime()
      // console.log(o)
      if (o.expire == -1 || o.expire > now) {
        return o.val
      } else {
        console.log(k + ' not found or has expired')
        localStorage.removeItem(k)
        return null
      }
    } catch (e) {
      console.log(e)
    }
  },
  remove(k) {
    localStorage.removeItem(k)
  }
}
export default storage
